home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 10 / BBS In A Box Volume X (AMUG) (January 1994).bin / Files / Prog / B-C / CStatusBar.cpt / CStatusBar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  3.9 KB  |  152 lines  |  [TEXT/KAHL]

  1. /*****
  2.  *     CStatusBar.c
  3.  *    Status bar graph class by Joe Zobkiw
  4.  *
  5.  *    This code is free and in the public domain, if you use it, please mention
  6.  *    so in your About Box. Do not distribute altered versions of this source but
  7.  *    feel free to mess with it in any way. This is just a quick hack but could be 
  8.  *    useful.
  9.  *
  10.  *    Suggestions or bugs: AFA Zobkiw @ America Online
  11.  *
  12.  *    NOTE: CStatusBar requires the following files to be included in the project:
  13.  *            - OSChecks.c
  14.  *            - MacTraps
  15.  *            - oops
  16.  *****/
  17.  
  18. #include "CStatusBar.h"
  19. #include "ColorToolbox.h"
  20.  
  21. /******************************************************************************
  22.  *    IStatusBar initializes the status bar.
  23.  *    dialog     is a DialogPtr to the dialog that the bar is in
  24.  *    item     is the item number of the bar (usually a user item)
  25.  *    vertical is TRUE if the bar is vertically oriented
  26.  *    shadow    is TRUE if we want a drop shadow
  27.  *    color    is TRUE is we want to define a fill color
  28.  *    rgb        is the fill color, ignored if (color == FALSE)
  29.  *
  30.  *****************************************************************************/
  31. void CStatusBar::IStatusBar( dialog, item, shadow, vertical, color, rgb )
  32.  
  33. DialogPtr    dialog;
  34. int            item;
  35. Boolean        shadow;
  36. Boolean        vertical;
  37. Boolean        color;
  38. RGBColor    rgb;
  39. {
  40.  
  41.     this->dialog = dialog;
  42.     this->item = item;
  43.     this->shadow = shadow;
  44.     this->vertical = vertical;
  45.     this->color = color;
  46.     this->rgb = rgb;
  47.     
  48.     Draw();
  49. }
  50.  
  51. /******************************************************************************
  52.  *    Draw draws the outline of the status bar
  53.  *
  54.  *****************************************************************************/
  55. void CStatusBar::Draw( void )
  56. {
  57.     GrafPtr        oldPort;
  58.     PenState    pen;
  59.     int            itemType;
  60.     Handle        itemHandle;
  61.     Rect        itemRect;
  62.     
  63.     GetPort( &oldPort );
  64.     GetPenState( &pen );
  65.     SetPort( dialog );
  66.     PenNormal();
  67.     
  68.     GetDItem( dialog, item, &itemType, &itemHandle, &itemRect );
  69.     PenPat( white );
  70.     PaintRect( &itemRect );
  71.     PenPat( black );
  72.     FrameRect( &itemRect );
  73.     
  74.     if (shadow) {
  75.         MoveTo( itemRect.left + SHADOW_DEPTH, itemRect.bottom );
  76.         LineTo( itemRect.right, itemRect.bottom );
  77.         LineTo( itemRect.right, itemRect.top + SHADOW_DEPTH );
  78.     }
  79.     
  80.     SetPort( oldPort );
  81.     SetPenState( &pen );
  82. }
  83.  
  84. /******************************************************************************
  85.  *    given a percentage of completion, Update will draw the status bar
  86.  *    filling either in color or in a gray pattern.
  87.  *
  88.  *    repeated calls to Update with a larger percentage is how the bar is animated
  89.  *
  90.  *****************************************************************************/
  91. void CStatusBar::Update( percent )
  92. int    percent;
  93. {
  94.     Rect        tempRect, myRect;
  95.     GrafPtr        oldPort;
  96.     PenState    pen;
  97.     int            itemType, rectLength, i, barFill;
  98.     Handle        itemHandle;
  99.     Rect        itemRect;
  100.     RGBColor    tempRgb;
  101.     
  102.     GetDItem( dialog, item, &itemType, &itemHandle, &itemRect );
  103.     tempRect = itemRect;
  104.     
  105.     GetPort( &oldPort );
  106.     GetPenState( &pen );
  107.     SetPort( dialog );
  108.     PenNormal();
  109.     
  110.     if ( ! this->vertical ) {
  111.         rectLength = tempRect.right - tempRect.left;
  112.         barFill = rectLength * .01 * percent;
  113.         if ( (tempRect.left + barFill + 2) > (tempRect.right - 1) )
  114.             barFill = tempRect.right - 2;
  115.         else
  116.             barFill = tempRect.left + barFill + 2;
  117.         SetRect(&myRect,tempRect.left + 1, tempRect.top + 1,
  118.                 barFill, tempRect.bottom - 1);
  119.     } else {
  120.         rectLength = tempRect.bottom - tempRect.top;
  121.         barFill = rectLength * .01 * percent;
  122.         if ( (tempRect.bottom - barFill - 2) < (tempRect.top + 1) )
  123.             barFill = tempRect.top + 2;
  124.         else
  125.             barFill = tempRect.bottom - barFill - 2;
  126.         SetRect(&myRect,tempRect.left + 1, barFill,
  127.                 tempRect.right - 1, tempRect.bottom - 1);
  128.     }
  129.  
  130.     if ( this->color )
  131.         if ( ColorQDIsPresent() ) {
  132.             tempRgb = rgb;
  133.             RGBForeColor(&tempRgb);
  134.         } else
  135.             PenPat( gray );
  136.     else
  137.         PenPat ( gray );
  138.         
  139.     PaintRect(&myRect);
  140.     
  141.     if (color) {
  142.         if ( ColorQDIsPresent() ) {
  143.             tempRgb.red = 0;
  144.             tempRgb.blue = 0;
  145.             tempRgb.green = 0;
  146.             RGBForeColor(&tempRgb);
  147.         }
  148.     }
  149.             
  150.     SetPort( oldPort );
  151.     SetPenState( &pen );
  152. }